<?php  
	
	namespace Home\Controller;
	use Think\Controller;
	
	class AffairStuController extends AllowController{
        // 投诉建议的的列表查询
        public function indexsstu(){
            //实例化
            $mod = M('complain');
            $mods = M('users');
            //获取用户id
            $id = $_SESSION['userid'];
            //总数
            $tot=$mod->where("type=1 AND sender=$id")->Count();

            //分页条数
            $nums = 5;
            $page = new \Think\Page($tot,$nums);

            $list = $mod->limit($page->firstRow,$page->listRows)->order('sendtime desc')->where("type=1 AND sender=$id")->select();
            //遍历 把时间戳转换日期
            foreach($list as $key=>$value){
                $list[$key]['sendtime'] = date("Y-m-d H:i:s",$value['sendtime']);

                //sender为0就是匿名发送
                if ($list[$key]['sender'] == 0) {
                   $list[$key]['sender'] = "匿名";
                }else{
                    //通过sender的用户id去关联users查询用户姓名
                   $name = $mods->where("id={$list[$key]['sender']}")->find();
                   $list[$key]['sender'] = $name["name"];
                }
            }
            if(empty($_GET['p'])){
                $p = 1;
            }else{
                $p = $_GET['p'];
            }
            //分配数据
            $this->assign('list',$list);
            $this->assign('curr',$p);
            $this->assign('page',ceil($tot/$nums));
            $this->assign('total',$tot);

            //导入模版
            $this->display("Complain/stucomplains");
        }

        //显示look模块
        public function lookstu(){
            $a = I('get.id');

            //实例化
            $mod = M('complain');
            $mods = M("users");           
            
            //查询单条语句
            $list=$mod->find($a);

            //获取发送人id
            $b = $list["sender"];

            //查询users下$b的name值
            $lists=$mods->field("name")->find($b);

            //获取sender的值进行判断
            if($b == 0){
                 $list["sender"] = '匿名';
            }else{
                 $list["sender"] = $lists['name'];
            }
            
            //分配数据
            $this->assign('list',$list);
            $this->display('Complain/stulook');
        }

        //查看更改投诉建议的信息
        public function news(){
            $names=$_SESSION['name'];

            $this->assign("names",$names);
            $this->display('Complain/news');
        }

        //插入数据
        public function insert(){
            //创建一个空数组
            $data = array();
            // 接受Ajax来的数据
            if ($_POST['uname'] == 1) {
                $data['sender']=0;
            }else{
                $data['sender']=$_SESSION["userid"];
            }
            // var_dump($_SESSION["name"]);
            $data['type'] = 1;
            $data['contents'] = $_POST['contents'];
            $data['status'] = 0;
            //获取时间戳
            $data['sendtime']=time();
            //实例化
            $mod = M("complain");

            if($mod->add($data)){
                echo "添加成功";
            }else{
                echo "添加失败";
            }             
        }		
	
       
      
    }
?>
